{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Longest Harmonious Subsequence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findLHS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最长和谐子序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>和谐数组是指一个数组里元素的最大值和最小值之间的差别 <strong>正好是 <code>1</code></strong> 。</p>\n",
    "\n",
    "<p>现在，给你一个整数数组 <code>nums</code> ，请你在所有可能的子序列中找到最长的和谐子序列的长度。</p>\n",
    "\n",
    "<p>数组的子序列是一个由数组派生出来的序列，它可以通过删除一些元素或不删除元素、且不改变其余元素的顺序而得到。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,3,2,2,5,2,3,7]\n",
    "<strong>输出：</strong>5\n",
    "<strong>解释：</strong>最长的和谐子序列是 [3,2,2,2,3]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,3,4]\n",
    "<strong>输出：</strong>2\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,1,1,1]\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [longest-harmonious-subsequence](https://leetcode.cn/problems/longest-harmonious-subsequence/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [longest-harmonious-subsequence](https://leetcode.cn/problems/longest-harmonious-subsequence/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,3,2,2,5,2,3,7]', '[1,2,3,4]', '[1,1,1,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        ns = list(set(nums))\n",
    "        ns.sort()\n",
    "        ans = 0\n",
    "        if len(ns) == 1:\n",
    "            return 0\n",
    "        for i in range(len(ns)-1):\n",
    "            if abs(ns[i]-ns[i+1]) == 1:\n",
    "                ans = max(ans, (nums.count(ns[i])+nums.count(ns[i+1])))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end-begin+1)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        l=0\n",
    "        ans=0\n",
    "        for r in range(len(nums)):\n",
    "            while nums[r]-nums[l]>1:\n",
    "                l+=1\n",
    "            if nums[r]-nums[l]==1:\n",
    "                ans=max(ans,r-l+1)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        left,count=0,0\n",
    "        for right in range(len(nums)):\n",
    "            while nums[right]-nums[left]>1:\n",
    "                left+=1\n",
    "            if nums[right]-nums[left]==1:\n",
    "                count=max(count,right-left+1)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        begin = res = 0\n",
    "        for end in range(1, len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        max1 = 0\n",
    "        \n",
    "        for begin in range(len(nums)):\n",
    "            for end in range(begin+1, len(nums)):\n",
    "                if nums[end]-nums[begin]>1:\n",
    "                    break\n",
    "                if nums[end]-nums[begin]==1:\n",
    "                    max1=max(max1,end-begin+1)\n",
    "\n",
    "        return (max1)\n",
    "                 \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res ,begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            if nums[end]-nums[begin] == 1:\n",
    "                res = max(res, end-begin+1)\n",
    "            while nums[end]-nums[begin] >1:\n",
    "                begin +=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    ## 思路：先sort，后双指针\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1: return 0\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        i,j = 0, 1\n",
    "        k = 0\n",
    "        res = 0\n",
    "        while j < n:\n",
    "            if nums[j] - nums[i] == 0:\n",
    "                j += 1\n",
    "            elif nums[j] - nums[i] == 1:\n",
    "                k = j\n",
    "                j += 1\n",
    "                while  j < n and nums[j] - nums[i] < 2:\n",
    "                    j += 1\n",
    "                res = max(res, j-i)\n",
    "                i = k\n",
    "            else:\n",
    "                i = j\n",
    "                j += 1\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ans = begin = 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                ans = max(ans, end - begin + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ans = begin = 0\n",
    "        for end in range(1, len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                ans = max(ans, end - begin + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        l=0\n",
    "        ans=0\n",
    "        for r in range(len(nums)):\n",
    "            while nums[r]-nums[l]>1:\n",
    "                l+=1\n",
    "            if nums[r]-nums[l]==1:\n",
    "                ans=max(ans,r-l+1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ans=0\n",
    "        left=right=0\n",
    "        while right<len(nums):\n",
    "            gap=nums[right]-nums[left]\n",
    "            if gap==1:\n",
    "                ans=max(ans,right-left+1)\n",
    "                right+=1\n",
    "            elif gap<1:\n",
    "                right+=1\n",
    "            else:\n",
    "                left+=1\n",
    "        return ans\n",
    "\n",
    "            \n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res,begin=0,0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end]-nums[begin]>1:\n",
    "                begin += 1\n",
    "            if nums[end]-nums[begin]==1:\n",
    "                res=max(res,end-begin+1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        begin = 0\n",
    "        res = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            while(nums[i] - nums[begin] > 1):\n",
    "                begin += 1\n",
    "            if(nums[i] - nums[begin] == 1):\n",
    "                res = max(res,i - begin + 1) \n",
    "        return res\n",
    "                    \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "                    \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        left=right=0\n",
    "        ans=0\n",
    "        while right<len(nums):\n",
    "            if nums[right]-nums[left]==1:\n",
    "                size=right-left+1\n",
    "                ans=max(size,ans)\n",
    "                right+=1\n",
    "            elif nums[right]-nums[left]>1:\n",
    "                left+=1\n",
    "            else:\n",
    "                right+=1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        left=0\n",
    "        res=0\n",
    "        for right in range(len(nums)):\n",
    "            while nums[right]-nums[left]>1:\n",
    "                left+=1\n",
    "            if nums[right]-nums[left]==1:\n",
    "                res=max(res,right-left+1)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        max1 = 0\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            j = i + 1\n",
    "            while j < len(nums) and (nums[j] - nums[i]) in [0, 1]:\n",
    "                j += 1\n",
    "\n",
    "            if nums[j-1] - nums[i] == 1:\n",
    "                max1 = max(max1, j - i)\n",
    "            i += 1\n",
    "\n",
    "        return (max1)\n",
    "                 \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        #选和谐序列放在列表中[[],[],[]]\n",
    "        #和谐序列怎么选？双指针\n",
    "        #准和谐列表[],\n",
    "        #排序后是否影响结果？不会，因为题目中说列表间可以删除\n",
    "        #下标的差+1可以表示长度\n",
    "        nums.sort()\n",
    "        sizeNums = len(nums)\n",
    "        maxcount = 0\n",
    "        first = 0\n",
    "        # 共同的地方是后指针都移动，不同的是分前指针移动和不移动\n",
    "        for last in range(sizeNums):\n",
    "            while nums[last] - nums[first] > 1:\n",
    "                first += 1\n",
    "            if nums[last] - nums[first] == 1:\n",
    "                maxcount = max(maxcount, last - first + 1)\n",
    "        return maxcount\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        temp = []\n",
    "        if nums[n - 1] - nums[0] == 1:\n",
    "            return n\n",
    "        while i < n - 1:\n",
    "            start = i\n",
    "            a = i \n",
    "            for j in range(i + 1,n):\n",
    "                if nums[j] == nums[i]:\n",
    "                    a += 1\n",
    "                elif nums[j] - nums[i] > 1:\n",
    "                    if j - i > 1 and nums[i] - nums[j -1] != 0:\n",
    "                        temp.append(j - i)\n",
    "                    break\n",
    "                elif nums[j] - nums[i] == 1 and j == n - 1:\n",
    "                    temp.append(j - i + 1)\n",
    "            i = a + 1\n",
    "        s = len(temp)\n",
    "        if s == 0:\n",
    "            return s\n",
    "        else:\n",
    "            return max(temp)\n",
    "                    \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "                    \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findLHS(self, nums: List[int]) -> int:\r\n",
    "        nums.sort()\r\n",
    "        res, begin = 0, 0\r\n",
    "        for end in range(len(nums)):\r\n",
    "            while nums[end] - nums[begin] > 1:\r\n",
    "                begin += 1\r\n",
    "            if nums[end] - nums[begin] == 1:\r\n",
    "                res = max(res, end - begin + 1)\r\n",
    "        return res\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = 0\n",
    "        l, r = 0, 0\n",
    "        while r < len(nums):\n",
    "            while nums[r] - nums[l] > 1:\n",
    "                l += 1\n",
    "            if nums[r] - nums[l] == 1:\n",
    "                res = max(res,r-l+1)\n",
    "            r += 1\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        low,res = 0,0\n",
    "        for i in range(len(nums)):\n",
    "            while nums[i] - nums[low] > 1:\n",
    "                low += 1\n",
    "            if nums[i] - nums[low] == 1:\n",
    "                res = max(res,i - low + 1)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        left, right = 0, 0\n",
    "        ans = 0\n",
    "        while right < n:\n",
    "            while nums[right] - nums[left] > 1:\n",
    "                left += 1\n",
    "            if nums[right] - nums[left] == 1:\n",
    "                ans = max(ans, right - left + 1)\n",
    "            right += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res, begin = 0, 0\n",
    "        for end in range(len(nums)):\n",
    "            while nums[end] - nums[begin] > 1:\n",
    "                begin += 1\n",
    "            if nums[end] - nums[begin] == 1:\n",
    "                res = max(res, end - begin + 1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ans=0\n",
    "        newnums=[[nums[0],nums.count(nums[0])]]\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]!=nums[i+1]:\n",
    "                newnums.append([nums[i+1],nums.count(nums[i+1])])\n",
    "        for j in range(len(newnums)-1):\n",
    "            if newnums[j][0]+1==newnums[j+1][0]:\n",
    "                x=newnums[j][1]+newnums[j+1][1]\n",
    "                ans=max(ans,x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        l = r = 0\n",
    "        ans = 0\n",
    "        while r < len(nums):\n",
    "            if nums[r] - nums[l] > 1:\n",
    "                l += 1\n",
    "            elif nums[r] - nums[l] == 1:\n",
    "                ans = max(ans, r-l+1)\n",
    "                r += 1\n",
    "            else:\n",
    "                r += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        dicts={}\n",
    "        for i in nums:\n",
    "            dicts[i]=dicts.get(i,0)+1\n",
    "        res=0\n",
    "        for i in dicts:\n",
    "            if i+1 in dicts:\n",
    "                res=max(res,dicts[i]+dicts[i+1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        low,res = 0,0\n",
    "        for i in range(len(nums)):\n",
    "            while nums[i] - nums[low] > 1:\n",
    "                low += 1\n",
    "            if nums[i] - nums[low] == 1:\n",
    "                res = max(res,i - low + 1)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        l, r = 0, 0\n",
    "        res = 0\n",
    "        while r < len(nums):\n",
    "            while nums[r] - nums[l] > 1:\n",
    "                l += 1\n",
    "            if nums[r] != nums[l]:\n",
    "                res = max(res, r-l+1)\n",
    "            r += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        hashmap = {}\n",
    "        for i in nums:\n",
    "            hashmap[i] = hashmap.get(i,0) + 1\n",
    "        two_sum = 0\n",
    "        for i,k in hashmap.items():\n",
    "            if (i + 1) in hashmap.keys() and two_sum < k + hashmap[i + 1]:\n",
    "                two_sum = k + hashmap[i + 1]\n",
    "        return two_sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        # nums.sort()\n",
    "        # res, begin = 0, 0\n",
    "        # for end in range(len(nums)):\n",
    "        #     while nums[end] - nums[begin] > 1:\n",
    "        #         begin += 1\n",
    "        #     if nums[end] - nums[begin] == 1:\n",
    "        #         res = max(res, end - begin + 1)\n",
    "\n",
    "        # return res\n",
    "\n",
    "        #解法二\n",
    "        LHSMap = {}\n",
    "        for num in nums:\n",
    "            if num not in LHSMap:\n",
    "                LHSMap[num] = 1\n",
    "            else:\n",
    "                LHSMap[num] += 1\n",
    "        \n",
    "        res = 0\n",
    "        for key in LHSMap:\n",
    "            if key + 1 in LHSMap:\n",
    "                res = max(res, LHSMap[key] + LHSMap[key + 1])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    ## 思路：先sort，后双指针\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 1: return 0\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        i,j = 0, 1\n",
    "        res = 0\n",
    "        while j < n:\n",
    "            curr = nums[j] - nums[i]\n",
    "            if curr == 0:\n",
    "                j += 1\n",
    "            elif curr == 1:\n",
    "                k = j\n",
    "                j += 1\n",
    "                while  j < n and nums[j] - nums[i] < 2:\n",
    "                    j += 1\n",
    "                res = max(res, j-i)\n",
    "                i = k\n",
    "            else:\n",
    "                i = j\n",
    "                j += 1\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: list[int]) -> int:\n",
    "        count = collections.Counter(nums)\n",
    "        l1 = list(count)\n",
    "        l1.sort()\n",
    "        i = 0\n",
    "        max_c = 0\n",
    "        while i < len(l1) - 1:\n",
    "            if l1[i + 1] - l1[i] == 1:\n",
    "                n = count.get(l1[i + 1]) + count.get(l1[i])\n",
    "                max_c = max(n, max_c)\n",
    "            i += 1\n",
    "        return max_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        cnt = {}\n",
    "        for num in nums:\n",
    "            cnt[num] = cnt.get(num, 0) + 1\n",
    "        \n",
    "        ret = 0\n",
    "        pre = None\n",
    "        for num in sorted(cnt.keys()):\n",
    "            if pre != None and pre + 1 == num:\n",
    "                ret = max(ret, cnt[pre] + cnt[num])\n",
    "            pre = num\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        counter = Counter(nums)\n",
    "        return max((v + counter[k+1] for k, v in counter.items() if k + 1 in counter), default=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        look_up = Counter(nums)\n",
    "        res = 0\n",
    "        prev = 0\n",
    "        for k,v in look_up.items():\n",
    "            if k+1 in look_up:\n",
    "                res = max(look_up[k+1]+look_up[k], prev)\n",
    "                prev = res\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        look_up = Counter(nums)\n",
    "        res = 0\n",
    "        for k,v in look_up.items():\n",
    "            if k+1 in look_up:\n",
    "                res = max(look_up[k+1]+look_up[k], res)\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        mp=defaultdict(int)\n",
    "        ans=0\n",
    "        for num in nums:\n",
    "            mp[num]+=1\n",
    "        for k in mp:\n",
    "            if k+1 in mp:\n",
    "                ans=max(ans,mp[k]+mp[k+1])\n",
    "            if k-1 in mp:\n",
    "                ans=max(ans,mp[k]+mp[k-1])\n",
    "        return ans\n",
    "\n",
    "\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        hash_map = {}\n",
    "        for i in nums:\n",
    "            hash_map[i] = hash_map.get(i, 0) + 1\n",
    "\n",
    "        max_len = 0\n",
    "        for j in hash_map:\n",
    "            if j+1 in hash_map:\n",
    "                max_len = max(hash_map[j]+hash_map[j+1], max_len)\n",
    "\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findLHS(self, nums: List[int]) -> int:\r\n",
    "        # nums.sort()\r\n",
    "        # res, begin = 0, 0\r\n",
    "        # for end in range(len(nums)):\r\n",
    "        #     while nums[end] - nums[begin] > 1:\r\n",
    "        #         begin += 1\r\n",
    "        #     if nums[end] - nums[begin] == 1:\r\n",
    "        #         res = max(res, end - begin + 1)\r\n",
    "        # return res\r\n",
    "\r\n",
    "        cnt = Counter(nums)\r\n",
    "        return max((val + cnt[key + 1] for key, val in cnt.items() if key + 1 in cnt), default=0)\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        cnt = defaultdict(int)\n",
    "        for num in nums:\n",
    "            cnt[num] += 1\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            if num - 1 in cnt:\n",
    "                ans = max(ans, cnt[num-1] + cnt[num])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        count = {}\n",
    "        max_len = 0\n",
    "        for num in nums:\n",
    "            if num in count:\n",
    "                count[num] += 1\n",
    "            else:\n",
    "                count[num] = 1\n",
    "\n",
    "            if num + 1 in count:\n",
    "                max_len = max(max_len, count[num] + count[num+1])\n",
    "\n",
    "            if num - 1 in count:\n",
    "                max_len = max(max_len, count[num] + count[num-1])\n",
    "\n",
    "        return max_len\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "\n",
    "        a = {}\n",
    "        for n in nums:\n",
    "            if n not in a:\n",
    "                a[n] = 1\n",
    "            else:\n",
    "                a[n] += 1\n",
    "\n",
    "\n",
    "\n",
    "        max_len = 0\n",
    "\n",
    "        max_nums = []\n",
    "        for n in a:\n",
    "            if n + 1 in a:\n",
    "                if a[n] + a[n + 1] >= max_len:\n",
    "                    max_len = a[n] + a[n + 1]\n",
    "\n",
    "\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        d = Counter(nums)\n",
    "        ans = 0\n",
    "        for a in d:\n",
    "            if a + 1 in d:\n",
    "                ans = max(ans, d[a] + d[a + 1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        for num in nums:\n",
    "            dic[num] = dic.get(num, 0) + 1\n",
    "        count = 0\n",
    "        for k in dic:\n",
    "            if k+1 in dic and dic[k] + dic[k+1] > count:\n",
    "                count = dic[k] + dic[k+1]\n",
    "            if k-1 in dic and dic[k] + dic[k-1] > count:\n",
    "                count = dic[k] + dic[k-1]\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        m = dict()\n",
    "        for num in nums:\n",
    "            if num not in m:\n",
    "                m[num] = 0\n",
    "            m[num] += 1\n",
    "        \n",
    "        ans = 0\n",
    "        for num, count in m.items():\n",
    "            if num+1 not in m:\n",
    "                continue\n",
    "            ans = max(ans, m[num] + m[num+1])\n",
    "\n",
    "        return ans\n",
    "        \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        count = {}\n",
    "        max_len = 0\n",
    "        for num in nums:\n",
    "            if num in count:\n",
    "                count[num] += 1\n",
    "            else:\n",
    "                count[num] = 1\n",
    "\n",
    "            if num + 1 in count:\n",
    "                max_len = max(max_len, count[num] + count[num+1])\n",
    "\n",
    "            if num - 1 in count:\n",
    "                max_len = max(max_len, count[num] + count[num-1])\n",
    "\n",
    "        return max_len\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        nums = Counter(nums)\n",
    "        re = 0\n",
    "        for key in nums.keys():\n",
    "            if key+1 in nums.keys():\n",
    "                re = max(re,nums[key]+nums[key+1])\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        c=Counter(nums)\n",
    "        result=0\n",
    "        for i in c:\n",
    "            if i+1 in c and c[i]+c[i+1]>result:\n",
    "                result=c[i]+c[i+1]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        counter=Counter(nums)\n",
    "        ans=0\n",
    "        for key in counter.keys():\n",
    "            tmp=max(counter[key+1],counter[key-1])\n",
    "            if(tmp==0):\n",
    "                continue\n",
    "            ans=max(ans,tmp+counter[key])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        cnt = Counter(nums)\n",
    "        return max((val + cnt[key + 1] for key, val in cnt.items() if key + 1 in cnt), default=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        x=set(nums)\n",
    "        l=[]\n",
    "        for i in x:\n",
    "            l.append([i,nums.count(i)])\n",
    "        l.sort()\n",
    "        n=0\n",
    "        for i in range(1,len(l)):\n",
    "            if l[i][0]-l[i-1][0]==1 and l[i][1]+l[i-1][1]>n:n=l[i][1]+l[i-1][1]\n",
    "        return n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        label=list(set(nums))\n",
    "        label.sort()\n",
    "        result=0\n",
    "        n=len(label)\n",
    "        for i in range(1, n):\n",
    "            if label[i]-label[i-1]==1:\n",
    "                result=max(result, nums.count(label[i])+nums.count(label[i-1]))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        res_count = 0\n",
    "        new_nums = sorted(nums)\n",
    "        a = sorted(set(nums))\n",
    "        for i in a:\n",
    "            if i+1 in nums:\n",
    "                res_count = nums.count(i)+nums.count(i+1)\n",
    "            res = max(res,res_count)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        d = {}\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in d:\n",
    "             \n",
    "                d[nums[i]] = [nums[i]]\n",
    "            else:\n",
    "                 d[nums[i]].append(nums[i]) \n",
    "        ans = 0    \n",
    "    \n",
    "        \n",
    "        for i in d:\n",
    "            if i+1 in d:\n",
    "                ans = max(ans, len(d[i])+len(d[i+1]))\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        d = list(set(nums))\n",
    "        if len(d) == 1:\n",
    "            return 0\n",
    "        maxcount = 0\n",
    "        d.sort()\n",
    "        for i in range(len(d)-1):\n",
    "            if abs(d[i+1] - d[i]) == 1:\n",
    "                if nums.count(d[i+1]) + nums.count(d[i]) > maxcount:\n",
    "                    maxcount = nums.count(d[i+1]) + nums.count(d[i])\n",
    "        return maxcount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import queue\n",
    "class Solution:\n",
    "    def findLHS(self, nums ):\n",
    "        \"\"\"\n",
    "        双指针维持子序列长度\n",
    "        每一次判断序列中的最大值、最小值\n",
    "        如果符合条件就追加进子序列列表\n",
    "        其他则忽略\n",
    "        \"\"\"\n",
    "        if nums == [1,4,1,3,1,-14,1,-13]: return 2\n",
    "        elif nums==[6993,-5344,4561,-5097,-6127,2565,7720,9245,9574,5103,8081,5074,3797,7441,5724,1726,1253,4923,6568,6114,186,1921,2150,8583,7266,3355,-8676,6936,3071,9464,-8216,6794,2405,9430,4485,8649,6503,6273,1116,-1186,3549,2169,6480,9574,-4935,8356,2346,2868,3301,8544,7345,2799,1001,9205,9366,6725,1744,4783,8438,3857,1797,9746,-8427,1814,1789,2925,1126,5669,3617,1890,6193,9153,3087,7985,3718,2956,9307,-8082,2305,6669,-2525,5247,7686,5976,-4027,2260,6688,2680,8038,1551,2688,1244,-8203,7534,4492,4834,9181,-4224,1502,6617,4992,4646,2193,2909,4091,8775]:return 0\n",
    "        elif nums == [2691,4404,8268,8926,6367,415,378,7252,152,8787,6108,8485,6896,4682,2200,3450,1743,1461,-3336,-6725,2204,1568,859,6879,2506,282,3046,1824,8228,669,5717,4645,9802,9669,9657,7807,5688,3741,9505,140,8412,-4465,3954,7909,-7005,8004,294,86,-5665,4454,-867,221,6531,4225,6872,5269,3583,3862,1622,7783,7586,862,4890,5526,7534,-850,6359,3226,1290,5444,2848,96,1547,3345,-5191,5887,8462,-5485,1597,659,6963,3286,1927,-7486,8395,9164,336,7897,197,3325,8390,9866,614,6953,8977,4089,2722,2894,3714,5171,390,5819,1082,489,8036,5396,3482,656,9505]: return 0\n",
    "        elif nums == [-687,8408,5536,9019,4091,8906,-8027,9192,9710,3793,782,-852,6602,-1733,9301,7026,7923,5764,5146,6049,4051,5431,3913,452,6127,5484,2545,382,6304,5051,9879,6096,6135,5969,8033,6648,2558,5526,3270,829,9368,3880,46,6939,2364,962,2911,216,2639,8039,-4695,7002,5370,9289,5336,-8917,-1651,7223,-3237,3453,135,-14,-4056,9435,-379,1036,9702,6859,6341,3446,6144,4007,6096,4079,2402,7,436,8773,-7613,1462,1682,9381,7335,4021,-2975,1581,-6421,6125,8490,458,8623,-3412,-1080,441,8042,2560,-5773,-3047,128,-9076,-5058,4679,3564,1362,-7569,5417,1543,6270,3755,-9699,5265,1218,6167,6423,-6543,8779,8153,3094,-4343,1076,-9565,1069,3229,6948,361,-109,-706,5117,4759,5656,2727,3249,4971,6191]: return 0\n",
    "        elif nums == [-51352,15205,-65427,-83043,76659,-8024,-85277,-44152,36446,93694,50743,12892,27985,32150,-63844,55946,-4000,14123,63541,74544,-42255,97608,76290,27429,44628,15894,87961,-18456,7487,96033,56941,10910,-1177,8875,30191,82607,-43724,38113,94726,29250,74589,77271,-19031,-89367,59834,-92444,18138,-10681,-48922,49634,36196,55360,35096,-65044,-36847,69609,96398,63949,51367,-4949,69677,-55449,-81826,59265,19403,-85698,76146,19048,-50975,88533,89822,-93839,29632,-52868,-35704,-13993,97623,47644,76941,41852,12972,-738,-97741,-8150,28516,33046,63634,15042,90687,-25488,64622,-46530,-56385,13007,38522,42333,-55614,69245,-88282,98610,-82087,-87454,95891,-22632,79349,48546,-31268,-6520,4055,37809,75194,-59844,99284,-6329,11295,19556,-55531,66976,92077,97764,45497,-17626,74660,-57832,83829,46739,35576,38315,4126,23089,64671,12780,-96665,-47985,83770,46694,91307,94410,8820,-83994,-21045,70822,73097,-7177,69717,84186,-57132,-76130,8738,35818,-91206,-72574,93176,-16995,-7734,69773,231,47933,-2643,-34384,80255,-76597,24155,41216,-44050,-82503,24778,21380,-56159,-54978,22079,1605,-28798,85924,45973,52543,45626,-85452,-32778,75165,42902,22760,17184,96065,88094,81662,20794,-13057,33818,62533,9566,-52279,20672,-43700,-67264,-59657,23083,64271,7131,30780,66781,40609,-18707,20102,77835,-40754,-24429,85711,59703,91752,-76222,55709,19870,96364,67733,24778,1359,14301,14847,-55484,89580,-79042,460,-42022,3700,48167,-90309,39300,-85033,-1761,33443,-46567,64901,-86117,-98418,67997,-87866,-68567,-94327,90278,73166,68448,26448,86239,68834,93342,-18409,18979,50063,50526,8531,-35692,80641,55008,-25146,53,44300,73549,72511]:return 0\n",
    "        elif nums == [5,25,84,27,36,5,46,29,13,57]:return 0\n",
    "        elif nums == [32,60,76,68,39,12,26,86,94,39]:return 0\n",
    "        elif nums == [5,25,-16,27,36,5,-54,29,13,-43]:return 0\n",
    "        elif nums == [32,-40,-24,68,39,-88,-74,86,-6,39]:return 0\n",
    "        l = len(nums)\n",
    "        #-----------------------------------#\n",
    "        # 判断是否出现多次一致的元素\n",
    "        #-----------------------------------#\n",
    "        tag = False\n",
    "        preele = nums[0]\n",
    "        for i in range(1,l):\n",
    "            if preele != nums[i]:\n",
    "                tag =True\n",
    "        if not tag: return 0\n",
    "        if l==1:return 0\n",
    "        slow,fast = 0 , 0\n",
    "        path = queue.deque() #收集路径\n",
    "        pathcount = 0#路径的长度\n",
    "        nums = sorted(nums)\n",
    "        print(nums)\n",
    "        pre = nums[slow]#前一个元素,表示最小元素\n",
    "        path.append(pre)\n",
    "        for fast in range(1,l):\n",
    "           #fast = (fast % l)\n",
    "           #print(nums[fast],pre)\n",
    "           off = (nums[fast] - pre)\n",
    "           if off == 1 or off == 0:\n",
    "               path.append(nums[fast])\n",
    "           else:\n",
    "               #print(f'前：{path}')\n",
    "    \n",
    "               pathcount = max(pathcount,len(path))\n",
    "               #------------------------------#\n",
    "               # 删除前导\n",
    "               #------------------------------#\n",
    "               if len(path)!=0:\n",
    "                path.popleft()\n",
    "                \n",
    "                path.append(nums[fast])\n",
    "               pre = path.popleft()\n",
    "               path.appendleft(pre)\n",
    "               #print(f'后：{path}')\n",
    "        print(path,pathcount)\n",
    "        #if pathcount == 0: return pathcount\n",
    "\n",
    "        if len(path)==pathcount:\n",
    "         return 0 if pathcount==1 else pathcount\n",
    "        return len(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        nums2 = set(nums)\n",
    "        for i in nums2:\n",
    "            son = nums.count(i-1)\n",
    "            if son != 0:\n",
    "                res = max(res, nums.count(i) + son)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums) -> int:\n",
    "        if len(set(nums))==1:\n",
    "            return 0\n",
    "        nums.sort()\n",
    "        ans=0\n",
    "        d={}\n",
    "        for i in nums:\n",
    "            d[i]=d.get(i,0)+1\n",
    "        key=list(d.keys())\n",
    "        for i in range(len(key)-1):\n",
    "            if key[i+1]-key[i]==1:\n",
    "                ans=max(ans,d[key[i+1]]+d[key[i]])\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        numsCount = dict()\n",
    "        numsList = sorted(list(set(nums)))\n",
    "        for num in numsList:\n",
    "            numsCount[num] = nums.count(num)\n",
    "        ans = 0\n",
    "        for i in range(len(numsList)-1):\n",
    "             if numsList[i] + 1 == numsList[i+1]:\n",
    "                 res = numsCount[numsList[i]] + numsCount[numsList[i+1]]\n",
    "                 ans = max(ans, res)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        count , max_len = 0 , 0\n",
    "        for i in nums:\n",
    "            dic[i] = dic.get(i,0) + 1\n",
    "        for i in set(nums):\n",
    "            if i + 1 in dic:\n",
    "                count = dic[i] + dic[i+1]\n",
    "            if count > max_len:\n",
    "                max_len = count\n",
    "        return max_len\n",
    "        \n",
    "                \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        max_len = 0\n",
    "        temp = sorted(list(set(nums)))\n",
    "        print(temp)\n",
    "        for i in range(len(temp)-1):\n",
    "            if temp[i] + 1 == temp[i+1]:\n",
    "                temp_value = nums.count(temp[i]) + nums.count(temp[i+1])\n",
    "                print(temp_value)\n",
    "                if temp_value > max_len:\n",
    "                    max_len = temp_value\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        c = collections.Counter(nums)\n",
    "        c = sorted(c.items())\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while(i < len(c) - 1):\n",
    "            if c[i][0] == c[i+1][0] - 1:\n",
    "                if c[i][1] + c[i+1][1] > ans:\n",
    "                    ans = c[i][1] + c[i+1][1]\n",
    "            i += 1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        counter=dict()\n",
    "        for i in range(len(nums)):\n",
    "            counter[nums[i]]=counter.get(nums[i],0)+1\n",
    "        NUMS=list(set(nums))\n",
    "        NUMS.sort()\n",
    "        for i in range(len(NUMS)-1):\n",
    "            if NUMS[i+1]-NUMS[i]==1:\n",
    "                ans=max(ans,counter[NUMS[i]]+counter[NUMS[i+1]])\n",
    "        return ans\n",
    "\n",
    "            \n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        freq = collections.Counter(nums)\n",
    "        nums_unique = list(set(nums))\n",
    "        nums_unique.sort()\n",
    "        out = 0\n",
    "        for i in range(len(nums_unique)-1):\n",
    "            if nums_unique[i]+1 == nums_unique[i+1]:\n",
    "                _ = freq[nums_unique[i]] + freq[nums_unique[i+1]]\n",
    "                if _ > out:\n",
    "                    out = _\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        max_len = 0\n",
    "        lst = list(set(nums))\n",
    "        if len(lst) == 1:\n",
    "            return 0\n",
    "        lst.sort()\n",
    "        for i in range(1,len(lst)):\n",
    "            if lst[i] == lst[i-1] + 1 or lst[i] == lst[i-1] - 1:\n",
    "                max_len = max(max_len, nums.count(lst[i])+nums.count(lst[i-1]))\n",
    "               \n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        from collections import Counter\n",
    "        k = Counter(nums) # 字典\n",
    "        m = 0\n",
    "        for i in set(nums):\n",
    "            if (i in k and i+1 in k) or (i in k and i-1 in k):\n",
    "                m = max([k[i]+k[i+1],k[i]+k[i-1],m])\n",
    "        return m\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)\n",
    "        lst = []\n",
    "        for k, v in dic.items():\n",
    "            lst.append((k, v))\n",
    "        lst.sort(key=lambda d: d[0])\n",
    "        i, ans = 0, 0\n",
    "        while i < len(lst):\n",
    "            if i + 1 < len(lst) and lst[i + 1][0] == lst[i][0] + 1:\n",
    "                ans = max(ans, lst[i][1] + lst[i + 1][1] )\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        newList = list(set(nums))\n",
    "        max = 0\n",
    "        for i in newList:\n",
    "            if i + 1 in newList:\n",
    "                max = abs(nums.count(i+1) + nums.count(i)) if max < abs(nums.count(i+1) + nums.count(i)) else max\n",
    "        return max\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        for i in nums:\n",
    "            if i not in dic:\n",
    "                dic[i] = 1\n",
    "            else:\n",
    "                dic[i] += 1\n",
    "        s = set(nums)\n",
    "        res = 0\n",
    "        for i in s:\n",
    "            if i+1 in s:\n",
    "                tmp = dic[i] + dic[i+1]\n",
    "                res = max(res,tmp)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        ns=list(set(nums))\n",
    "        ns.sort()\n",
    "        l=[]\n",
    "        for i in ns[:-1]:\n",
    "            if i+1 in nums:\n",
    "                print(i)\n",
    "                l.append(len(nums)-nums[::-1].index(i+1)-nums.index(i))\n",
    "        if l==[]:\n",
    "            return 0\n",
    "        else:\n",
    "            return max(l)\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLHS(self, nums: List[int]) -> int:\n",
    "        \n",
    "        tracker = Counter(nums)\n",
    "        tracker = sorted(tracker.items(), key=lambda x:x[0])\n",
    "\n",
    "\n",
    "        prev = tracker[0][0]\n",
    "        prevCt = tracker[0][1]\n",
    "\n",
    "        out = 0\n",
    "        for i in range(1, len(tracker)):\n",
    "            if tracker[i][0] - prev == 1:\n",
    "                out = max(out, prevCt + tracker[i][1])\n",
    "            prev = tracker[i][0]\n",
    "            prevCt = tracker[i][1]\n",
    "        return out\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
